package cn.didi.feidi.javabackend.mysql.jpa.controller;

import cn.didi.feidi.javabackend.mysql.jpa.repository.WaterDeliverJpaRep;
import cn.didi.feidi.javabackend.mysql.jpa.waterDeliver.WaterDeliver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lindanpeng on 2017/7/16.
 */
@RestController
@RequestMapping("WaterDeliver")
public class WaterDeliverController {
    @Autowired
    WaterDeliverJpaRep waterDeliverJpaRep;

    //为了使用criteria多条件查询与多属性动态更新，此处渗透HibernateTemplate
//    @Autowired
    HibernateTemplate hibernateTemplate = new HibernateTemplate();

    @RequestMapping(value="getById",method = RequestMethod.GET)
    public WaterDeliver getWaterDeliverById(@RequestParam String waterDeliverId){
        WaterDeliver WaterDeliver = waterDeliverJpaRep.findOne(waterDeliverId);
        return WaterDeliver;
    }
    @RequestMapping(value="getByPhoneNumber",method=RequestMethod.GET)
    public WaterDeliver getWaterDeliverByPhoneNumber(@RequestParam String phoneNumber){
        WaterDeliver WaterDeliver = waterDeliverJpaRep.findByPhoneNumber(phoneNumber);
        return WaterDeliver;
    }
    @RequestMapping(value="add",method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody WaterDeliver waterDeliver){
        waterDeliverJpaRep.save(waterDeliver);
        return "OK";
    }
    @RequestMapping(value="update",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody WaterDeliver waterDeliver){
        waterDeliverJpaRep.delete(waterDeliver.getWaterDeliverId());
        waterDeliverJpaRep.save(waterDeliver);
        return "OK";
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(@RequestParam String waterDeliverId){
        waterDeliverJpaRep.delete(waterDeliverId);
        return "OK";
    }



}
